var window = floaty.window(
    <frame>
        <card w="200" h="*" cardCornerRadius="5dp" cardElevation="0dp" gravity="center" layout_gravity="center">
            <vertical w="*" h="*" bg="#66000000">
                <text id="title" textSize="16" h="40" textColor="#ffffff" textStyle="bold" text="游戏助手"gravity="center"/>
                <View w="*" h="1" bg="#E0E0E0"/>

                <horizontal h="40" w="*" margin="8 0 8 0">
                    <text text="间隔时间：" textColor="#E0E0E0" gravity="center_vertical" textSize="14" w="0" layout_weight="1"/>
                    <input id="start_time" textSize="14sp" textColor="#E0E0E0" inputType="number" focusable="true" w="0" h="*"  text="3000" hint="0" layout_weight="1" />
                    <text textSize="14sp"  text="毫秒" textColor="#E0E0E0" gravity="center_vertical"  w="30" h="*"/>
                </horizontal>
                <horizontal h="40" w="*" margin="8 0 8 0">
                    <text text="执行次数：" textColor="#E0E0E0" gravity="center_vertical" textSize="14" w="0" layout_weight="1"/>
                    <input id="all_numbers" textSize="14sp" textColor="#E0E0E0" inputType="number" focusable="true" w="0" h="*"  text="300" hint="0" layout_weight="1" />
                    <text textSize="14sp"  text="次" textColor="#E0E0E0" gravity="center_vertical"  w="30" h="*"/>
                </horizontal>
                <View w="*" h="1" bg="#E0E0E0" margin="0 8 0 8"/>
                <horizontal h="30" w="*" layout_gravity="center" gravity="center" margin="0 0 0 8">
                    <card w="60" h="30" cardCornerRadius="3dp" cardElevation="0dp" layout_gravity="center" marginRight="8">
                        <text id="start" text="开启" h="*" w="*" textColor="#FFFFFF"  gravity="center" textSize="13sp" bg="#FF1CBBB4" />
                    </card>
                    
                    <card w="60" h="30" cardCornerRadius="3dp" cardElevation="0dp" layout_gravity="center" marginLeft="8">
                        <text id="stopUi" text="关闭" h="*" w="*" textColor="#FFFFFF" gravity="center" textSize="13sp" bg="#ff0000" />
                    </card>
                </horizontal>
            </vertical>
        </card>
    </frame>
);


window.setPosition(200, 200);
window.setSize(-2,-2);
window.exitOnClose();

setInterval(() => { }, 1000);

setTimeout(openConsole,500);
//判断当前线程是否启动
var start_status = true;
var start_slide = true;
//已经执行次数
var num_ = 0;

window.title.on("click", () => {
    //开启位置调整
    window.setAdjustEnabled(!window.isAdjustEnabled());
});

window.start_time.on("touch_down", () => {
    window.requestFocus();
    window.start_time.requestFocus();
});


//退出
window.stopUi.on("click", () => {
    console.hide();
    window.close();
    floaty.closeAll();
    threads.shutDownAll();
    exit();
});

window.start.click(()=>{
    if(start_status){//开启
        window.start.setText("停止");
        var numbers = parseInt(window.all_numbers.getText().toString());
        console.info("程序执行"+numbers+"次后将停止运行！");
        if(start_slide){
            threads.start(run);
        }else{
            start_slide = true;
        }
    }else{//停止
        console.log("程序暂停执行，等待中...");
        setTimeout(stop, 500);
    }
});

/**
 * 设置网络图片地址
 * @param {图片加载地址}} url 
 */
function setBg(url){
    new com.stardust.autojs.core.ui.inflater.util.Drawables().loadInto(window.fab_img, 
        android.net.Uri.parse(url));
}

/**
 * 暂停
 */
function stop(){
    window.start.setText("开启");
    start_slide = false;
    start_status = true;
}

/**
 * 启动
 */
function run(){
    start_status = false;
    // 一直保持屏幕常亮
    device.keepScreenOn();
    //检查是否有截图权限

    if(!requestScreenCapture()){
        toast("请求截图失败！");
        exit();
    }
    
    var numbers = parseInt(window.all_numbers.getText().toString());
    setInterval(function(){
        if(start_slide){
            if(num_==numbers){
                console.info("程序执行完毕！");
                setTimeout(stop, 500);
            }
            huadong();
        }
    },1500);
}

/**
 * 控制台
 */
function openConsole(){

    //显示控制台
    var middle = device.width / 2 - 400;
    console.setSize(800, 800);
    console.setPosition(middle, 80);
    console.show();
    console.setTitle("游戏助手");
}

/**
 * 获取计算位置
 */
function huadong(){
    var time_random = parseInt(window.start_time.getText().toString());
    //截图
    var img = captureScreen();
    var reStart = findColor(img, "#3dc4b5", {
        region: [347, 1944],
        threshold: 4
    });

    var statr =  findColor(img, "#FFB21B", {
        region: [360, 2222],
        threshold: 4
    });
    //执行开始
    if(statr){
        num_ = num_+1;
        console.info("执行开始事件："+num_+"次");
        click(365,2240);
        sleep(time_random);
    }
    // else{
    //     console.info("未获取到位置start");
    // }

    if(reStart){
        console.info("执行重新开始事件。");
        click(347,1960);
        sleep(time_random);
    }
    // else{
    //     console.info("未获取到位置reStart");
    // }
}

